package vip.liux.contracts.shared.kits;

import jakarta.annotation.Nonnull;
import jakarta.annotation.PostConstruct;

import java.util.Collection;

public class ProfileKit {

    public static final String DEV = "dev";
    public static final String PROD = "prod";
    public static final String TEST = "test";
    private static String[] profiles = new String[0];

    public static boolean isDev() {
        for (String profile : profiles) {
            if (DEV.equals(profile)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isProd() {
        for (String profile : profiles) {
            if (PROD.equals(profile)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isTest() {
        for (String profile : profiles) {
            if (TEST.equals(profile)) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasProfile(@Nonnull String profile) {
        for (String p : profiles) {
            if (profile.equals(p)) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasAnyProfile(@Nonnull Collection<String> profiles) {
        for (String profile : profiles) {
            if (hasProfile(profile)) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasAllProfiles(@Nonnull Collection<String> profiles) {
        for (String profile : profiles) {
            if (!hasProfile(profile)) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasAnyProfile(@Nonnull String... profiles) {
        for (String profile : profiles) {
            if (hasProfile(profile)) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasAllProfiles(@Nonnull String... profiles) {
        for (String profile : profiles) {
            if (!hasProfile(profile)) {
                return false;
            }
        }
        return true;
    }

    @PostConstruct
    public void init() {
        profiles = System.getProperty("spring.profiles.active", DEV).split(",");
    }
}
